<?php
// used for navigation (#currentpage) in style.css
$info = pathinfo($_SERVER['REQUEST_URI']);
$current_file_name = basename($_SERVER['REQUEST_URI'], ".".$info['extension']);

//******* Meeting/Group/Location ADD and UPDATE *************//
function processTable($table)	{
	$query_string = "";
	// Set timezone and set current date and time for use as MySQL editdate
	$currentdate = date("Y-m-d H:i:s");
	$kv = array();
	foreach ($_POST as $key => $value) //put all the form variables into an array
			{
				if($key != 'submit' && $key != 'id' && $key != 'currentFile') //don't process certain POST vars
				{
					$kv[] = "$key='$value'";
					$k[] = "$key";
					$v[] = "'$value'";
				}
			}
	   
	if ($_POST['id'])	// IF the form has been submitted and this is and EDIT to and EXISTING RECORD
			// add the current time and date here rather than passing it from the form
	{		if ($table == 'users'){
				$kv[] = "last_login ='$currentdate'"; 
			}else {
				$kv[] = "editdate='$currentdate'";  
			}
			$query_string = join(", ", $kv);
			$query_string = "UPDATE ".$table." SET ".$query_string." WHERE id='".$_POST['id']."'";
			checkForm($table);
			$result = mysql_query($query_string) or die(mysql_error());
			$_SESSION['message'] = 'Updated';
	}elseif($_POST['submit']) {	 // If the form has been submitted and this is a NEW RECORD
			if ($table == 'users'){
				$kv[] = "last_login =''"; 
			}else {
				$kv[] = "editdate='$currentdate'";  
			}
			$keys = join(", ", $k);
			$values = join(", ", $v);
			$query_string = "INSERT INTO ".$table." (".$keys.") VALUES (".$values.")";
			checkForm($table);
			$result = mysql_query($query_string) or die(mysql_error());
			if ($table == 'groups'){
				$_SESSION['message'] = 'newgroup';
			}
	}
}
// check to make sure form is filled out correctly
function checkForm($table) {
	switch($table)	{
		case "meetings":
			if(!$_POST['day'] | !$_POST['mtime'] | !$_POST['group_id'] | !$_POST['location_id'] | !$_POST['open_closed'] | 
			!$_POST['mtg_type_id'] | !$_POST['men_women'] | !$_POST['hc']) {
				die('You did not fill in a required field. Please <a href="meetingsform.php" title="Meetings Form">try again</a>');
			}
			break;
		case "groups":
			if(!$_POST['groupname']) {
				die('You did not fill in a required field GROUP NAME. Please <a href="groupform.php" title="Login">try again</a>');
			}
			// Check to see if that groupname is already in use
			if (!$_POST['id']) {
				$group_sql = "SELECT groupname FROM groups  ORDER BY groupname ASC";
				$group_result =  mysql_query($group_sql) or die(mysql_error());
				$num_rows = mysql_num_rows($group_result);
				while ($group_row = mysql_fetch_assoc($group_result)) {
					if ($group_row['groupname'] == $groupname){
						die('<font color="red">Group Name already in use. Please <strong><a href="groupform.php" title="Login">try again</a></strong>');
						}
				}
			}
			break;	
	}
}
// return day name based on the number set in the table
function getDay($dayNum) {
	switch ($dayNum)
	{
		case 1:
			$dayName="Sunday";
			return $dayName;
		case 2:
			$dayName="Monday";
			return $dayName;
		case 3:
			$dayName="Tuesday";
			return $dayName;
		case 4:
			$dayName="Wednesday";
			return $dayName;
		case 5:
			$dayName="Thursday";
			return $dayName;
		case 6:
			$dayName="Friday";
			return $dayName;
		case 7:
			$dayName="Saturday";
			return $dayName;
	}
}
// alert message
function alert($message)	{
echo "<script>alert(".$message.");</script>";
}

function displayTime($theTime,$dbtime) {
echo "<select name='$theTime' size='1'>";
echo  "<option value=''>None</option>";
		//this section generates times, currently 7AM ($hour = 7) to 11PM ($endtime = mktime(23, 15))
		//note, had to add 15 mins to end time to get while loop logic to end at 11 instead of 10:45
		$hour = 7;
		$min = 0;
		$startime = mktime($hour, $min, 0);
		$endtime = mktime(24, 15, 0);
		$nexttime = $startime;
		
		while ($startime != $endtime){//create time every 15 minutes and select if valuetime is passed (edit)
			$selecttime = date('h:iA',$nexttime);
			$valuetime = date('H:i:s',$nexttime);
			echo "<option ";
			if ($dbtime==$valuetime){// if the time is set then display it
				echo "selected=\"selected\"";}
				echo  "value=\"$valuetime\">$selecttime</option>";
			if ($min == 45){
				$min = 0;
				$hour = $hour + 1;
					}else{
				$min = $min + 15;	
						}	
			$nexttime= mktime($hour, $min, 0);
			$startime=$nexttime;
		}
echo "</select>";
}
// Random password gebnerator for new users
function createRandomPassword() {
			$chars = "abcdefghijkmnopqrstuvwxyz023456789";
			srand((double)microtime()*1000000);
			$i = 0;
			$pass = '' ;
			while ($i <= 7) {
				$num = rand() % 33;
				$tmp = substr($chars, $num, 1);
				$pass = $pass . $tmp;
				$i++;
			}
			return $pass;
		}
function addhttp($url) {
    if (!preg_match("~^https?://~i", $url)) {
        $url = "http://" . $url;
    }
	if (substr($url, -1) != "/") {
		 $url = $url . "/";
	}
    return $url;
}
// handle undefined variables
function ifsetor(&$variable, $default = null) {
    if (isset($variable)) {
        $tmp = $variable;
    } else {
        $tmp = $default;
    }
    return $tmp;
}
// repportrowcolor
$reprowcolor = '#DDD';
?>